<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title> For Loops </title>

  <link rel="stylesheet" media="screen" href="/css/style.css" >
</head>
<body>

  <header class="site-header">
    <h3 class="site-title">
      <a href="https://mkaz.github.io/working-with-go/">
        Working with Go
      </a>
    </h3>
  </header>

  <main>

    <aside>
      <h2>Contents</h2>
      <ul>
        
          <li><a href="/hello.html">Hola Mundo</a></li>
        
          <li><a href="/strings.html">Strings</a></li>
        
          <li><a href="/slices.html">Slices</a></li>
        
          <li><a href="/functions.html">Functions</a></li>
        
          <li><a href="/control-structures.html">Control Structures</a></li>
        
          <li><a href="/for-loops.html">For Loops</a></li>
        
          <li><a href="/structs.html">Structs</a></li>
        
          <li><a href="/maps.html">Maps</a></li>
        
          <li><a href="/files.html">Files</a></li>
        
          <li><a href="/read-dir.html">Directories</a></li>
        
          <li><a href="/objects.html">Objects</a></li>
        
          <li><a href="/command-line.html">Command-line</a></li>
        
          <li><a href="/date-time.html">Date/Time</a></li>
        
          <li><a href="/sorting.html">Sorting</a></li>
        
          <li><a href="/json.html">JSON</a></li>
        
          <li><a href="/web-server.html">Web Server</a></li>
        
          <li><a href="/guess-game.html">Guessing Game</a></li>
        
          <li><a href="/rest-api.html">REST API</a></li>
        
          <li><a href="/regexps.html">Regexp</a></li>
        
          <li><a href="/memcache.html">Memcache</a></li>
        
          <li><a href="/mysql.html">MySQL</a></li>
        
          <li><a href="/interfaces.html">Interfaces</a></li>
        
          <li><a href="/testing.html">Testing</a></li>
        
      </ul>
    </aside>

    <article class="entry-content">
      <h1>Loops</h1>

<p>Go uses <code>for</code> for all loops, there is no <code>while</code> or <code>do-while</code> loops.</p>

<h2>For Loop</h2>

<pre><code class="language-go">
alphas := []string{&quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;}

// standard for loop
for i := 0; i &lt; len(alphas); i++ {
	fmt.Printf(&quot;%d: %s \n&quot;, i, alphas[i])
}

// counting backwards
for i := len(alphas)-1; i &gt;= 0; i-- {
	fmt.Printf(&quot;%d: %s \n&quot;, i, alphas[i])
}
</code></pre>

<h2>Using Range</h2>

<p>Iterating over an array is easier using <code>range</code> function.</p>

<pre><code class="language-go">for i, val := range alphas {
	fmt.Printf(&quot;%d: %s \n&quot;, i, val)
}
</code></pre>

<p>If you did not care about the value and only wanted the index</p>

<pre><code class="language-go">for i := range alphas {
	fmt.Println(i)
}
</code></pre>

<p>If you did not care about the index and only the value, you need to use the <code>_</code> character.</p>

<pre><code class="language-go">for _, val := range alphas {
	fmt.Println(val)
}
</code></pre>

<h2>Use For like  While</h2>

<pre><code class="language-go">x := 0
for x &lt; 10 {
	fmt.Println(x)
	x++
}
</code></pre>

<h2>Infinite Loop</h2>

<pre><code class="language-go">for {
	fmt.Print(&quot;.&quot;)
}
</code></pre>

    </article>

  </main>

<footer class="site-footer" role="contentinfo">
    An <a href="https://mkaz.blog/">mkaz</a> joint
    <span class="sep"> | </span>
    <a href="https://github.com/mkaz/hasite">Powered by Hastie</a>
</footer>

<script src="/js/asciinema/asciinema-player.js"></script>
<script src="/js/prism.js"></script>

</body>
</html>